The code to import images and train resnet50 with them is currently working. This is an initial test to see if the network is actually learning with the current image classes.
The first step to use this code is to label stacks. This is done with choose_best.py. This program opens RisWidget and loads all the images in one stack in a flipbook so the best focus can be identified. The function best() then saves a .txt file in the stack folder with the file name of the best focus image.
sort_images_5_cat.py then uses this .txt file to calculate the number of steps off each image in the stack is and sort it into one of 5 classes: very_out_neg slightly_out_neg acceptable slightly_out_pos very_out_pos
These are the classes that resnet50 is trained on.
from pathlib import Path
import os
import glob
import shutil
# Specify a directory to copy the sorted images into
sorted_dir = Path('/Users/zplab/Desktop/VeraPythonScripts/vera_autofocus/microscope_images/')
# Make folders for the 3 classes
(sorted_dir / 'train' / 'acceptable').mkdir()
(sorted_dir / 'train' / 'slightly_out_neg').mkdir()
(sorted_dir / 'train' / 'very_out_neg').mkdir()
(sorted_dir / 'train' / 'slightly_out_pos').mkdir()
(sorted_dir / 'train' / 'very_out_pos').mkdir()
(sorted_dir / 'test' / 'acceptable').mkdir()
(sorted_dir / 'test' / 'slightly_out_neg').mkdir()
(sorted_dir / 'test' / 'very_out_neg').mkdir()
(sorted_dir / 'test' / 'slightly_out_pos').mkdir()
(sorted_dir / 'test' / 'very_out_pos').mkdir()
train_test_flipper = 'train' # Use this variable to alternate saving images in the train and test folders
sorted_dir = sorted_dir / train_test_flipper
# Set a directory to search in
experiment_dir = Path('/Volumes/purplearray/Pittman_Will/20190521_cyclo_dead/')
# In order to access a purple array directory from Squidward include /mnt/purplearray/
# instead of Volumes/purplearray
# Set the step ranges here to make them easy to change in the future
acceptable = 1
slightly_out = 6
# Keep track of how many images and classes have been generated
image_counter = 0
class_counter = 5 # There are 5 classes in this version of the sorter
# Iterate through all sub directories looking for best_focus.txt files.
for worm in os.listdir(experiment_dir):
worm_dir = experiment_dir / worm
for stack in worm_dir.glob('* focus'):
stack_dir = worm_dir / stack
q = stack_dir / 'best_focus.txt'
# Check if best focus has been noted for this stack
if q.exists():
# Switch between saving images in the train and test folders
sorted_dir = sorted_dir.parent
if train_test_flipper == 'train':
train_test_flipper = 'test'
else:
train_test_flipper = 'train'
sorted_dir = sorted_dir / train_test_flipper
# Read the filename of the best focus image out of the textfile, then convert the
# filename (ex. 20.png) into an integer
best_focus = int(str(q.read_text()).split('.')[0])
# Go through the directory to find all .png images
for image in stack_dir.glob('*.png'):
# Convert the stem of the filename into an int, and calculate the distance (in steps)
# from that image to the best focus plane
distance = int(image.stem) - best_focus
# Use the distance to decide which folder the image should be saved in
if distance < (-1 * slightly_out):
class_folder = sorted_dir / 'very_out_neg'
elif distance < (-1 * acceptable):
class_folder = sorted_dir / 'slightly_out_neg'
elif distance <= acceptable:
class_folder = sorted_dir / 'acceptable'
elif distance <= slightly_out:
class_folder = sorted_dir / 'slightly_out_pos'
else:
class_folder = sorted_dir / 'very_out_pos'
name_counter = 0
image_name = str(image.stem) + '_' + str(name_counter) + '.png'
# Check if the image name + counter already exists in the folder. If it does, increase
# the counter to get a unique image name
while (class_folder / image_name).exists():
name_counter = name_counter + 1
image_name = str(image.stem) + '_' + str(name_counter) + '.png'
# Save the image file in the appropriate class folder
shutil.copy(str(image), (str(class_folder) + '/' + image_name))
# Update the image counter
image_counter = image_counter + 1
print('sort_images found ' + str(image_counter) + ' images and sorted them into '
+ str(class_counter) + ' classes')
Look at some images from the different categories to verify that the sorter is working: very_out_neg
from IPython.display import Image
Image("/Users/zplab/Desktop/VeraPythonScripts/vera_autofocus/microscope_images/train/very_out_neg/00_2.png")